--[[
Licenced under GNU GPL.
For more information, see LICENCE in the main folder.
--]]
require "./AI/USER_AI/easyai/ai/Command.lua"
require "./AI/USER_AI/easyai/ai/Global.lua"
require "./AI/USER_AI/easyai/ai/State.lua"
require "./AI/USER_AI/easyai/objects/Enemy.lua"
require "./AI/USER_AI/easyai/objects/Homun.lua"
require "./AI/USER_AI/easyai/objects/Owner.lua"

--[[
Main AI of the system.
WARNING: Only edit this file if you know what you're doing.

@version	0.0 23 Aug 2011
@author		rodmp
--]]
startUpDone = false

function AI(myid)
    local msg = GetMsg(myid)
    local rmsg = GetResMsg(myid)

    if (not startUpDone) then
        TraceAI("EASY SYSTEM IS STARTING UP...")

        -- Setup homunculus.
        homun = Homun:new()
        homun:setID(myid)
        homun:setHomunType()
        homun:setAttackRange()

        -- Setup owner.
        owner = Owner:new()
        owner:setID(GetV(V_OWNER, myid))

        -- Setup enemy.
        myEnemy = Enemy:new()

        -- Setup state.
        myState = State:new()
        myState:setCurrentState(0)	-- 0 means idle state.

        -- Activate plugin if available.
        --usePlugin()

        startUpDone = true
        TraceAI("STARTUP FINISHED.")
    end

    if msg[1] == NONE_CMD then
        if rmsg[1] ~= NONE_CMD then
            if List.size(ResCmdList) < 10 then
                List.pushright (ResCmdList,rmsg)
            end
        end
    else
        List.clear (ResCmdList)
        ProcessCommand (msg)
    end

    if (myState:getCurrentState() == 0) then			-- Idle state.
        myState:doIdle()
    elseif (myState:getCurrentState() == 1) then		-- Follow state.
        myState:doFollow()
    elseif (myState:getCurrentState() == 2) then		-- Chase state.
        myState:doChase()
    elseif (myState:getCurrentState() == 3) then		-- Attack state.
        myState:doAttack()
    end
end